﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI.EditPages
{
    public partial class ViewContracts : BasePage
    {
        protected bool contractType
        {
            get { return (bool)ViewState["contractType"];}
            set { ViewState["contractType"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            lstContract.OnClientUpdate = "return showUpdateMemo(this);";
            if (!IsPostBack)
            {
                LoadNewContracts();
            }

            lnkEditContract.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
            lnkDeleteContract.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
            if (Branches.CurrentBranchID != SessionUser.BranchID)
            {
                lnkEditContract.OnClientClick = "";
                lnkDeleteContract.OnClientClick = "";
            }
        }

        private void LoadNewContracts()
        {
            contractType = true;
            View_Contracts contracts = View_Contracts.GetNewContract();
            lstContract.SourceObject = contracts;
            lstContract.SortExpression = View_Contracts.ColumnNames.CreatedDate;
            lstContract.SortDirection = BaseListControl.SORT_DESC;
            lstContract.Refresh();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "try{selectNewContract();}catch(ex){}", true);
        }

        public string GetUpdateMemoPageLink()
        {
            return "UpdateMemo.aspx?key=" + lstContract.SourceObjectSessionID;
        }

        public void btnNewContract_OnClick(object sender, EventArgs e)
        {
            LoadNewContracts();
        }

        public void btnRefresh_OnClick(object sender, EventArgs e)
        {
            lstContract.Refresh();
        }

        public string GetEditContractLink()
        {
            return this.ResolveClientUrl("~/EditPages/EditContractDate.aspx") + "?key=" + lstContract.SourceObjectSessionID;
        }

        public void btnPaidContract_OnClick(object sender, EventArgs e)
        {
            LoadPaidContracts();
        }

        private void LoadPaidContracts()
        {
            contractType = false;
            View_Contracts contracts = View_Contracts.GetPaidContract();
            lstContract.SourceObject = contracts;
            lstContract.SortExpression = View_Contracts.ColumnNames.CreatedDate;
            lstContract.SortDirection = BaseListControl.SORT_DESC;
            lstContract.Refresh();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "try{selectPaidContract();}catch(e){}", true);
        }

        protected void btnEditContract_OnClick(object sender, EventArgs e)
        {
            lstContract.Refresh();
            if(contractType)
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "try{selectNewContract();}catch(ex){}", true);
            else
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "try{selectPaidContract();}catch(e){}", true);
        }

        protected void btnDeleteContract_OnClick(object sender, EventArgs e)
        {
            View_Contracts contracts = (View_Contracts)lstContract.SourceObject;
            try
            {
                if (lstContract.SelectedValue != "")
                {
                    contracts.Filter = View_Contracts.ColumnNames.ID + "=" + lstContract.SelectedValue;
                    Invoices.RevertInvoice(contracts.InvoiceID, SessionUser.ID);
                    contracts.MarkAsDeleted();
                    lstContract.CurrentPageIndex = 0;
                    contracts.Filter = "";
                    lstContract.Refresh();
                    
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ViewContracts.aspx.cs", "btnDeleteContract_OnClick", ex.Message + ex.StackTrace);
                contracts.Filter = "";
                ((Popup)Master).ShowErrorMessage(ex.Message);
            }

            if (contractType)
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "try{selectNewContract();}catch(ex){}", true);
            else
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), "try{selectPaidContract();}catch(e){}", true);
        }
    }
}
